package com.qlh.composewidget

import android.content.ComponentName
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.blankj.utilcode.util.GsonUtils
import com.feeyo.groundservice.base_util.formatTimeYMDHM
import com.feeyo.groundservice.composewidget.usage.base.BaseComposeActivity
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace

/**
 *作者：70893 on 2025-05-21
 *描述：
 */
class SendBroadcastActivity : BaseComposeActivity() {

    companion object{
        val feeyoData = mutableStateOf<GtcBO?>(null)
    }

    private val feeyoReceiver = DealFeeyoReceiver()
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        registerReceiver(feeyoReceiver, IntentFilter().apply {
            addAction(DealFeeyoReceiver.ACTION)
        }, RECEIVER_EXPORTED)

        setContent {
            Column {
                Text(
                    text = "发送数据到调度",
                    modifier = Modifier.intervalClick {
                        Intent().apply {
                            setAction("com.feeyo.msg")
                            setPackage("com.feeyo.groundservice.hak")

                            //参数
                            putExtra("type", "node")//数据类型
                            putExtra("taskId", "123")//任务ID，飞友提前传过来的，GTc只需原样返回即可
                            putExtra("action", "del")//add 录入节点时间 del 删除节点时间

                            sendBroadcast(this)
                        }
                    }
                )

                VerticalSpace(R.dimen.d30.dimenToDp())

                Text(
                    text = "拉起后台第三方APP",
                    modifier = Modifier.intervalClick {
                        packageManager.getLaunchIntentForPackage("com.feeyo.groundservice.lhw_guide")
                            ?.apply {
                                //setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                                //putExtra("type", "node")
                                startActivity(this)
                            }
                    }
                )

                VerticalSpace(R.dimen.d30.dimenToDp())

                Text(
                    text = "冷启动第三方APP",
                    modifier = Modifier.intervalClick {
                        Intent().apply {
                            val componentName = ComponentName(
                                "com.feeyo.groundservice.lhw_guide",
                                "com.feeyo.groundservice.activity.MainActivity"
                            )
                            setComponent(componentName)
                            putExtra("type", "node")
                            startActivity(this)
                        }
                    }
                )


                //测试节点数据的
                VerticalSpace(R.dimen.d30.dimenToDp())
                feeyoData.value?.node?.forEach {
                    Text(
                        text = it.title +"--${(it.ntime*1000).formatTimeYMDHM()}",
                        modifier = Modifier.size(100.dp,50.dp).background(
                            color = R.color.blue_4796F4.toComposeColor()
                        ).intervalClick {
                            Intent().apply {
                                setAction("com.feeyo.groundservice.node.msg")
                                setPackage("com.feeyo.groundservice.lhw_guide")

                                //参数
                                putExtra("type", "node")//数据类型
                                putExtra(
                                    "taskId",
                                    feeyoData.value?.task?.taskId
                                )//任务ID，飞友提前传过来的，GTc只需原样返回即可
                                putExtra("nodeId", it.id)
                                putExtra(
                                    "action",
                                    if (it.ntime > 0) "del" else "add"
                                )//add 录入节点时间 del

                                sendBroadcast(this)

                            }
                        }
                    )

                    VerticalSpace(10.dp)
                }
            }
        }

        //
        val data = intent.getStringExtra("task")
        if (data != null) {
            val gtcBO = GsonUtils.fromJson(data, GtcBO::class.java)
            feeyoData.value = gtcBO
        }
    }


    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        val data = intent.getStringExtra("task")
        if (data != null) {
            val gtcBO = GsonUtils.fromJson(data, GtcBO::class.java)
            feeyoData.value = gtcBO
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(feeyoReceiver)
    }

}